package com.example.testspring;

import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @Author：anguangpu
 * @Date：2024/5/21 17:58
 * @Description:
 */
@RestControllerAdvice
public class GlobalExceptionHandler {
    @ExceptionHandler({Exception.class})
    public ReturnError handleException(Exception e) {
        if (e instanceof BusinessException) {
            BusinessException businessException = (BusinessException) e;
            return new ReturnError(businessException.getCode(), businessException.getMessage());
        } else {
            return new ReturnError(ReturnError.ERROR_CODE, ReturnError.ERROR_MSG);
        }
    }
}
